﻿Public Class Profile
    Inherits System.Web.UI.Page
    Public dbmanager = New DBManager
    Private checkDisplay As Boolean

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            'Load
            Dim display_name = Request.QueryString("id")
            Try
                Dim userProfile As userAcc = dbmanager.retrieveUser(display_name)
                lblDisplayEmail.Text = userProfile.email
                tbxDisplayName.Text = userProfile.display_name
                lblDisplayGender.Text = userProfile.gender
                Try
                    tbxFirstName.Text = userProfile.first_name
                Catch ex As Exception
                    tbxFirstName.Text = ""
                End Try
                Try
                    tbxLastName.Text = userProfile.last_name
                Catch ex As Exception
                    tbxLastName.Text = ""
                End Try
                If userProfile.date_of_birth.ToString = "1/1/0001 AM 12:00:00" Then
                    tbxDOB.Text = ""
                Else
                    tbxDOB.Text = userProfile.date_of_birth.ToShortDateString
                End If
                Try
                    ddlCountry.Text = userProfile.country
                Catch ex As Exception
                    ddlCountry.Text = "Unselected"
                End Try
            Catch ex As Exception
                MsgBox("Please login first", MsgBoxStyle.OkOnly, "Please login")
                Response.Redirect("home.aspx")
            End Try
            btnConfirm.Visible = False
        End If
       
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim display_name = tbxDisplayName.Text
        If MsgBox("Are you sure you want to delete your account?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
            dbmanager.deleteUser(display_name)
            MsgBox("Account Deleted")
            Response.Redirect("home.aspx")
        End If
    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        tbxDisplayName.Enabled = True
        tbxPassword.Enabled = True
        tbxRetypePassword.Enabled = True
        tbxFirstName.Enabled = True
        tbxLastName.Enabled = True
        tbxDOB.Enabled = True
        ddlCountry.Enabled = True
        btnConfirm.Visible = True
        btnUpdate.Visible = False
    End Sub


    Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConfirm.Click
        Dim temp_date As Date
        Dim display_name = Request.QueryString("id")
        If (tbxDisplayName.Text = display_name) Then
            dbmanager.updateUser(tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, tbxDOB.Text, ddlCountry.Text)
            MsgBox("You have updated your profile!")

        ElseIf (tbxDisplayName.Text.Length > 3) Then
            checkDisplay = dbmanager.checkAvailDisplayName(tbxDisplayName.Text)
            If checkDisplay = False Then
                lblWarningName.Visible = True
                lblWarningName.Text = "Display Name is not available"
                lblAvailName.Visible = False

            Else
                Try
                    dbmanager.updateUser(lblDisplayEmail.Text, tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, tbxDOB.Text, ddlCountry.Text)
                    MsgBox("You have updated your profile!")
                Catch ex As Exception
                    Try
                        dbmanager.updateUser(tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, temp_date, ddlCountry.Text)
                        MsgBox("You have updated your profile!")
                    Catch ex2 As Exception
                        MsgBox("You have not updated your profile successfully")
                    End Try
                End Try
            End If
        End If

        tbxFirstName.Enabled = False
        tbxDisplayName.Enabled = False
        tbxPassword.Enabled = False
        tbxRetypePassword.Enabled = False
        tbxFirstName.Enabled = False
        tbxLastName.Enabled = False
        tbxDOB.Enabled = False
        ddlCountry.Enabled = False
        btnConfirm.Visible = False
        btnUpdate.Visible = True
    End Sub

    Protected Sub btnChange_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnChange.Click
        Try
            dbmanager.changePassword(tbxDisplayName.Text, tbxPassword.Text)
            MsgBox("Your new password is changed")
        Catch ex As Exception
            MsgBox("Your password is not changed")
        End Try

    End Sub


    Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
        If (tbxDisplayName.Text.Length > 3) Then
            checkDisplay = dbmanager.checkAvailDisplayName(tbxDisplayName.Text)
            If checkDisplay = False Then
                lblWarningName.Visible = True
                lblWarningName.Text = "Display Name is not available"
                lblAvailName.Visible = False
                tbxDisplayName.ReadOnly = False
            Else
                lblAvailName.Visible = True
                lblAvailName.Text = "Display Name is available"
                lblWarningName.Visible = False
                tbxDisplayName.ReadOnly = True
            End If
        Else
            lblWarningName.Visible = True
            lblWarningName.Text = "Display Name must be 4 or more charcters"
        End If
    End Sub
End Class